package com.java.ui;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.SwingUtilities;

import com.java.dao.CommentDao;
import com.java.entity.Comment;
import com.java.ext.MyButton;
import com.java.model.CommentTableModel;

/**
* @ClassName: MyCommentWindow
* @Description: 我的留言
* @author ChenJiaLu
* @date 2023年12月29日 下午2:55:56
*
 */
public class MyCommentWindow extends BaseJFrame {
	private static final long serialVersionUID = 1L;

	private List<Comment> comments = new ArrayList<Comment>();

	private Comment selectObj = null;

	private JTable table = null;

	public MyCommentWindow() {
		setTitle("医院就诊管理系统——患者（查看留言）");
		setSize(700, 480);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setLocationRelativeTo(null);
		initMenu(this);

		CommentDao dDao = new CommentDao();
		comments = dDao.getPatientComments();

		CommentTableModel md = new CommentTableModel(comments);
		table = new JTable(md);
		JScrollPane scrollPane = new JScrollPane(table);
		add(scrollPane, BorderLayout.CENTER);

		// 创建输入框和按钮
		JPanel inputPanel = new JPanel();
		inputPanel.setLayout(new FlowLayout(FlowLayout.RIGHT));
		final MyButton delButton = new MyButton("删除").danger();
		inputPanel.add(delButton);
		add(inputPanel, BorderLayout.SOUTH);
		delButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if (null == selectObj) {
					error("请选中");
					return;
				}
				CommentDao dDao = new CommentDao();
            	dDao.delComment(selectObj.getId());
            	CommentDao sDao = new CommentDao();
            	comments = sDao.getPatientComments();
				table.setModel(new CommentTableModel(comments));
            	ok("删除成功");
			}
		});
		table.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				if (e.getClickCount() == 1) {
					int row = table.rowAtPoint(e.getPoint());
					selectObj = getObj(row);
					delButton.setEnabled(true);
				}
			}
		});

		setVisible(true);
	}

	public Comment getObj(int index) {
		for (int i = 0; i < comments.size(); i++) {
			if (index == i) {
				return comments.get(i);
			}
		}
		return null;
	}

	public static void main(String[] args) {
		try {
			SwingUtilities.invokeAndWait(new Runnable() {
				public void run() {
					new DoctorCommentWindow();
				}
			});
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}